package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.system.system.SysUser;
import com.gec.system.util.Result;
import com.gec.system.util.MDHelper;
import com.gec.system.vo.AssignRoleVo;
import com.gec.system.vo.SysUserQueryVo;
import com.gec.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dafei
 * @since 2024-09-21
 */
@Api(tags = "用户管理控制器")
@RestController
@RequestMapping("/admin/system/user")
public class SysUserController {
    @Autowired
    SysUserService service;
    @ApiOperation("分页的接口")
    @GetMapping("/{page}/{limit}")
    public Result page(@PathVariable("page") Long pageIndex, @PathVariable() Long limit, SysUserQueryVo vo){
        IPage<SysUser> page = new Page<>(pageIndex, limit);
        page =service.selectPage(page,vo);
        return Result.ok(page);

    }
    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    public Result addUser(@RequestBody SysUser user){
        String encrypt = MDHelper.encrypt(user.getPassword());
        user.setPassword(encrypt);
        boolean isOK = this.service.save(user);
        return isOK?Result.ok():Result.fail();
    }
    @ApiOperation("修改用户")
    @PostMapping("/updateUserById")
    public Result updateRole(@RequestBody SysUser user){
        boolean isOK = this.service.updateById(user);
        return isOK?Result.ok():Result.fail();
    }
    @ApiOperation("更新状态")
    @PostMapping("/updateStatus")
    public Result updateStatus(@RequestBody SysUser user){
        boolean isOK = this.service.updateStauts(user);
        return isOK?Result.ok():Result.fail();
    }
    @ApiOperation("删除用户")
    @DeleteMapping("/delete/{id}")
    public Result deleteByID(@PathVariable Integer id){
        boolean isOK = service.removeById(id);
        return isOK?Result.ok():Result.fail();

    }
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable("userId") Long userId) {
        Map<String, Object> roleMap = service.getRolesByUserId(userId);
        return Result.ok(roleMap);
    }
    @ApiOperation(value = "根据用户获取分配数据")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssignRoleVo assignRoleVo) {
        service.doAssign(assignRoleVo);
        return Result.ok();
    }

}

